package com.elm.daoimpl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.elm.dao.ShoppingCarDao;
import com.elm.model.ShoppingCar;
import com.elm.util.JDBCUtil;
/**

 * 

 * @author pgm

 * 

 */
public class ShoppingCarDaoImpl implements ShoppingCarDao{

	@Override
	public List<ShoppingCar> getAllFoodIdByUserid(int userid) {
		List<ShoppingCar> cars = new ArrayList<ShoppingCar>();
		Connection con = JDBCUtil.getConnection();
		StringBuffer sql = new StringBuffer("select * from shoppingcar where userid = ? and isdel=0");
		try {
			PreparedStatement pst = con.prepareStatement(sql.toString());
			pst.setInt(1, userid);
			ResultSet rs = pst.executeQuery();
			while(rs.next()){
				ShoppingCar car = new ShoppingCar();
				car.setCarid(rs.getInt("carid"));
				car.setFoodid(rs.getInt("foodid"));
				car.setUserid(userid);
				cars.add(car);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block


			e.printStackTrace();
		}
		return cars;
	}

	@Override
	public int emptyShoppingCar(int userid) {
		int i = 0;
		Connection con = JDBCUtil.getConnection();
		StringBuffer sql = new StringBuffer("update shoppingcar set isdel = 1 where userid =?");
		try {
			PreparedStatement pst = con.prepareStatement(sql.toString());
			pst.setInt(1, userid);
			i = pst.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block


			e.printStackTrace();
		}
		return i;
	}
	@Override
	public List<Integer> getShoppingCarFoodidsByCarids(List<Integer> ordercarids) {
		List<Integer> cars = new ArrayList<Integer>();
		Connection con = JDBCUtil.getConnection();
		StringBuffer sql = new StringBuffer("select * from shoppingcar where carid = ?");
		for (int i = 0; i < ordercarids.size(); i++) {
			try {
				PreparedStatement pst = con.prepareStatement(sql.toString());
				pst.setInt(1, ordercarids.get(i));
				ResultSet rs = pst.executeQuery();
				if(rs.next()){
					cars.add(rs.getInt("foodid"));
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		return cars;
	}

	@Override
	public List<Integer> getShoppingCarnumfoodByCarids(List<Integer> ordercarids) {
		List<Integer> numfood = new ArrayList<Integer>();
		Connection con = JDBCUtil.getConnection();
		StringBuffer sql = new StringBuffer("select * from shoppingcar where carid = ?");
		for (int i = 0; i < ordercarids.size(); i++) {
			try {
				PreparedStatement pst = con.prepareStatement(sql.toString());
				pst.setInt(1, ordercarids.get(i));
				ResultSet rs = pst.executeQuery();
				if(rs.next()){
					numfood.add(rs.getInt("numfood"));
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		return numfood;
	}

	@Override
	public boolean addToShoppingCar(int userid, int foodid) {
		// TODO Auto-generated method stub
		boolean flag = false;
		Connection con = JDBCUtil.getConnection();
		StringBuffer sql = new StringBuffer("insert into shoppingcar (foodid,userid,numfood) values(?,?,1)");
		try {
			PreparedStatement pst = con.prepareStatement(sql.toString());
			pst.setInt(1, foodid);
			pst.setInt(2, userid);
			int i = pst.executeUpdate();
			if(i>0){
				flag = true;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return flag;
	}
	@Override
	public void changeNumFoods(String[] fnumbers, List<ShoppingCar> cars) {
		Connection con = JDBCUtil.getConnection();
		for (int i = 0; i < fnumbers.length; i++) {
			StringBuffer sql = new StringBuffer("update shoppingcar set numfood = ? where carid = ?");
			try {
				PreparedStatement pst = con.prepareStatement(sql.toString());
				pst.setInt(1, Integer.parseInt(fnumbers[i]));
				pst.setInt(2, cars.get(i).getCarid());
				pst.executeUpdate();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	@Override
	public int deleteCarInfo(int carid) {
		int i = 0;
		Connection con = JDBCUtil.getConnection();
		StringBuffer sql = new StringBuffer("delete from shoppingcar where carid = ?");
		try {
			PreparedStatement pst = con.prepareStatement(sql.toString());
			pst.setInt(1, carid);
			i = pst.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return i;
	}
}
